package com.kay;

import com.kay.dao.OrderMapper;
import com.kay.dao.UserMapper;
import com.kay.pojo.Order;
import com.kay.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description:延迟加载
 * @Author: Kay
 * @CreateDate: 2021/7/1$ 21:02$
 * @UpdateUser: Kay
 * @UpdateDate: 2021/7/1$ 21:02$
 */
public class LazyLoadTest {
    private UserMapper userMapper;
    private OrderMapper orderMapper;
    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession(true);
        userMapper = sqlSession.getMapper(UserMapper.class);
        orderMapper = sqlSession.getMapper(OrderMapper.class);
    }

    @Test
    public void testLazy(){
        Order order = orderMapper.findById(1);
        System.out.println("查询结束 获取订单时间~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println(order.getOrderTime());
        System.out.println("准备获取User对象~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println(order.getUser());
    }
}
